#include "Ogre.h"
#include <float.h>




#include "Main/Logger.h"
#include "Graphics/MapBlock.h"
#include "Graphics/MaterialAtlas.h" 



using namespace Graphics;

std::string MapBlock::XMLHandler::cDefaultMeshName("unknown_block");
std::string MapBlock::XMLHandler::cDefaultMaterialName("Blocks/Unknown");

MapBlock::XMLHandler::XMLHandler(Ogre::SceneManager *manager,MaterialAtlas *atlas):
	mWorkingId(-1),mAdditionalObjectMode(false),mManager(manager),mMaterialAtlas(atlas)
{}


void MapBlock::XMLHandler::elementStart(const CEGUI::String& element, const CEGUI::XMLAttributes& attributes)
{
	if (element == "Block")
	{
		if (!attributes.exists("id")) 
		{
			mWorkingId = -1;
			return;
		}
		mWorkingId = attributes.getValueAsInteger("id");
		mBlockMap.resize(mWorkingId+1);
		mBlockMap[mWorkingId] = new MapBlock;
		mBlockMap[mWorkingId]->mId = mWorkingId;
		std::string message = "Adding block definition for id =" + Ogre::StringConverter::toString(mWorkingId); 
		Main::Logger::getSingleton().logMessage(message,std::string("MapBlock::XmlHandler"));
		return;
	}
	if (mWorkingId == -1) return;
	if (element == "AdditionalObject")
	{
		mAdditionalObjectMode = true;
		return;
	}
	mElement = element.c_str();
}
void MapBlock::XMLHandler::elementEnd(const CEGUI::String& element)
{
	if (mWorkingId == -1) return;
	if (element == "Block") 
	{
		mWorkingId = -1;
		return;
	}
	if (element == "AdditionalObject")
	{

		mAdditionalObjectMode = false;
		return;
	}
	mElement = ""; 
}
void MapBlock::XMLHandler::text(const CEGUI::String& text)
{
	if (mWorkingId == -1) return;
	if (mElement == "") return;
	if (mElement == "Description")
	{
		mBlockMap[mWorkingId]->mDescription = text.c_str();
		return;
	}
	if (mElement == "isFloor")
	{
		mBlockMap[mWorkingId]->mIsFloor = Ogre::StringConverter::parseBool(text.c_str());
		return;
	}
	if (mElement == "isWall")
	{
		mBlockMap[mWorkingId]->mIsWall = Ogre::StringConverter::parseBool(text.c_str());
		return;
	}
	if (mElement == "isEmpty")
	{
		mBlockMap[mWorkingId]->mIsEmpty = Ogre::StringConverter::parseBool(text.c_str());
		return;
	}
	if (mElement == "isSlope")
	{
		mBlockMap[mWorkingId]->mIsSlope = Ogre::StringConverter::parseBool(text.c_str());
		return;
	}
	if (mElement == "DefaultMatgloss")
	{
		if (text.empty()) return;
		WORD idx = mMaterialAtlas->getMaterialIndexByName(std::string(text.c_str()));
		if (idx == 0xFFFF) return;
		mBlockMap[mWorkingId]->mHasDefaultMatgloss = true;
		mBlockMap[mWorkingId]->mDefaultMatgloss = idx;

	}
	if (mAdditionalObjectMode)
	{
		if (mElement == "Mesh")
		{
			mBlockMap[mWorkingId]->mAdditionalObject = mManager->createEntity(
				"Block"+Ogre::StringConverter::toString(mWorkingId),
				text.c_str());
		}
		if (mElement == "Material")
		{
			mBlockMap[mWorkingId]->mAdditionalMaterial = Ogre::MaterialManager::getSingleton().getByName(text.c_str());  
		}
	}
}

std::vector<MapBlock*> &MapBlock::XMLHandler::retrieveBlockMap()
{
	return mBlockMap;
}



MapBlock::MapBlock()
: mAdditionalObject(0),mIsEmpty(false),mIsWall(false),mIsFloor(false),mIsSlope(false),
  mHasDefaultMatgloss(false),mDefaultMatgloss(0)
{};

MapBlock::~MapBlock()
{};


Ogre::Entity *MapBlock::getEntity(/*some stuff about setting materials*/)
{
	//mEntity->setMaterialName(mMaterial->getName());
	
	//return mEntity;
	return NULL;
}

